package xm.components.text
{
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Timer;
	
	import xm.interfaces.ILifeCycle;
	import xm.manager.StageManager;
	import xm.manager.ToolTipManager;
	import xm.util.TextFieldUtil;
	
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class TruncateTextField extends TextField implements ILifeCycle
	{
		
		
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * truncate 가능여부
		 */		
		public var enableTruncate : Boolean = true;
		
		/**
		 * truncate 되기 전 원본 String
		 */		
		public var originString : String = "";
		
		/**
		 * truncate 되었는지, 여부. 
		 */		
		public var truncated : Boolean = false;
		
		/**
		 * 잘린영역의 마크 default : "..."  
		 */		
		public var truncateMark : String = "...";
		
		/**
		 * truncate 시키기 위한 너비 
		 */		
		public var viewWidth : Number = 0;
		
		/**
		 * 텍스트의 뒤를 ... 으로 자를건지 여부. ( isFalse == 앞 ) 
		 */		
		public var isTruncateToBack : Boolean = true;
		
		
		/**
		 * 터치 다운이 된 후 툴팁이 뜨기가지의 지연시간. 
		 */		
		public var delayToShowTooltip : Number = 1000;
		
		//-----------------------------------------------------------------------------------
		// protected vars
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		
		private var timer : Timer;
		
		
		//-----------------------------------------------------------------------------------
		// constructor
		//-----------------------------------------------------------------------------------
		
		/**
		 * TruncateTextField constructor
		 * <br />
		 * selectable 의 기본값은 false
		 * <br />
		 * wordwrap이 true 이면 truncated 는 작동하지 않는다.
		 * <br />
		 * 
		 */		
		public function TruncateTextField()
		{
			super();
			
			this.selectable = false;
			this.createChildren();
			this.creationComplete();
			
			
			timer = new Timer( delayToShowTooltip, 1 );
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete );
		}
		
			
		
		//-----------------------------------------------------------------------------------
		// public function
		//-----------------------------------------------------------------------------------
		
		
		
		/**
		 * @param str
		 */
		public function setText( str:String ):void
		{
			originString = str;
			
			if( enableTruncate )
			{
				setTruncateText();
			}	
		}
		
		
		
		/**
		 * 현재 있는 originString 을 truncate 적용하고자 할때.
		 * <br />
		 * viewWidth 등이 변경된 경우 호출하는 메서드.
		 * <br />
		 * <br />
		 * apply
		 */		
		public function apply():void
		{
			if( enableTruncate )
			{
				setTruncateText();
			}
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * createChildren
		 */		
		public function createChildren():void
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onStage );
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage );
		}
				
		
		/**
		 * createComplete
		 */		
		public function creationComplete():void
		{
			
		}
		
		
		/**
		 * add
		 */		
		public function add():void
		{
			
		}
		
		
		/**
		 * remove
		 */
		public function remove():void
		{
			
		}
		
		/**
		 * destroy
		 */		
		public function destroy():void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onStage );
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage );
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onDown );
			StageManager.getInstance().stage.removeEventListener( MouseEvent.MOUSE_UP, onOut );
			
			enableTruncate = false;
			originString = null;
			truncated = false;
			truncateMark = null;
			viewWidth = NaN;
			isTruncateToBack = false;
			
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete );
			if( timer.running ) 
			{
				timer.stop();
			}
			timer = null;
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * ... 텍스트 삽입.
		 * @param str
		 */		
		public function set textWithTruncate( str : String ):void
		{
			setText( str );
		}
		
		
		//-----------------------------------------------------------------------------------
		// protected function
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		// private function
		//-----------------------------------------------------------------------------------
		
		private function setTruncateText():void
		{
			this.text = originString;
			this.wordWrap = false;
			
			truncated = TextFieldUtil.truncateByWidth( this, viewWidth, truncateMark, isTruncateToBack );
			
			if( truncated )
			{
				this.addEventListener(MouseEvent.MOUSE_DOWN, onDown );
//				this.addEventListener(MouseEvent.MOUSE_OVER, onDown );
//				this.addEventListener( MouseEvent.MOUSE_UP, onOut );
//				this.addEventListener(MouseEvent.MOUSE_OUT, onOut );
			}
			else
			{
				this.removeEventListener(MouseEvent.MOUSE_DOWN, onDown );
//				this.removeEventListener(MouseEvent.MOUSE_OVER, onDown );
//				this.removeEventListener( MouseEvent.MOUSE_UP, onOut );
//				this.removeEventListener(MouseEvent.ROLL_OUT, onOut );
			}
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		/**
		 * @param e
		 */		
		protected function onRemoveFromStage(e:Event):void
		{
			remove();
		}
		
		/**
		 * @param e
		 */		
		protected function onStage(e:Event):void
		{
			add();
		}
		
		/**
		 * @param e
		 */		
		protected function onOut(e:MouseEvent):void
		{
			StageManager.getInstance().stage.removeEventListener( MouseEvent.MOUSE_UP, onOut );
			
			Mouse.cursor = MouseCursor.AUTO;
			ToolTipManager.instance.hideTooltip();
			
			if( timer.running )
			{
				timer.stop();
			}
		}
		
		
		
		
		
		/**
		 * @param e
		 */		
		protected function onDown(e:MouseEvent):void
		{
			timer.reset();
			timer.delay = delayToShowTooltip;
			timer.start();
		}		
		
		
		protected function onTimerComplete(e:TimerEvent):void
		{
			Mouse.cursor = MouseCursor.ARROW;
			ToolTipManager.instance.showTooltip( originString );
			
			StageManager.getInstance().stage.addEventListener( MouseEvent.MOUSE_UP, onOut );
		}	

	}
}